package cn.com.aiton.common.utils;

import cn.com.aiton.common.model.ServiceResult;
import cn.com.aiton.common.model.gbt209992007.GbtPatternDomain;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by Administrator on 2016/12/2.
 */
public class JsonToPatternListUtil {

    private static final ObjectMapper objectMapper = new ObjectMapper();
    private static final Logger LOG =
            LoggerFactory.getLogger(JsonToPatternListUtil.class);

    public static ServiceResult<GbtPatternDomain> convertTimePattern(String json) {
        ServiceResult<GbtPatternDomain> webMessage = new ServiceResult<>();
        try {
           // GbtPatternDomain gbtTimePatternCommand = JSON.parseObject(json, GbtPatternDomain.class);
            GbtPatternDomain gbtPatternDomain = objectMapper.readValue(json, GbtPatternDomain.class);
            webMessage.setObject(gbtPatternDomain);
            webMessage.setSuccess(true);
            webMessage.setMessage("转换成功");
            return webMessage;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            webMessage.setSuccess(false);
            webMessage.setMessage("系统异常");
            return webMessage;
        }

    }
    public static ServiceResult<List<GbtPatternDomain>> convertTimePatternLists(String json) {
        ServiceResult<List<GbtPatternDomain>> result = new ServiceResult<>();
        try {
            //List<GbtPatternDomain> list = JSON.parseArray(json,GbtPatternDomain.class);
            List<GbtPatternDomain> listll = objectMapper.readValue(json, new TypeReference<List<GbtPatternDomain>>(){});
            result.setMessage("转换成功");
            result.setObject(listll);
            result.setSuccess(true);
            return result;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            result.setMessage("转换失败");
            result.setObject(null);
            result.setSuccess(false);
            return result;
        }

    }
}
